#include "mypushbutton.h"
#include <QMouseEvent>
#include <QtDebug>
#include <QPainter>

MyPushButton::MyPushButton(QWidget* parent):QPushButton(parent){
    m_action = BntAction::normal;
    m_width = 32;
    m_height = 32;
    update();
}

MyPushButton::MyPushButton(const QString& normalImage,const QString& pressImage,
                           const QString& hoverImage,QWidget *parent):QPushButton(parent){
    this->m_normalImage = normalImage;
    this->m_hoverImage = hoverImage;
    this->m_pressImage = pressImage;
    m_action = BntAction::normal;
    update();
}

void MyPushButton::mousePressEvent(QMouseEvent* ev){
    m_action = BntAction::press;
    update();
    return QPushButton::mousePressEvent(ev);
}

void MyPushButton::enterEvent(QEvent *event){
    m_action = BntAction::hover;
    update();
    //QMouseEvent* ev = static_cast<QMouseEvent*>(event);
    QPushButton::enterEvent(event);
}

void MyPushButton::leaveEvent(QEvent *event){
    m_action = BntAction::normal;
    update();
    //QMouseEvent* ev = static_cast<QMouseEvent*>(event);
    QPushButton::leaveEvent(event);
}

void MyPushButton::mouseReleaseEvent(QMouseEvent* ev){
    m_action = BntAction::hover;
    update();
    return QPushButton::mouseReleaseEvent(ev);
}

void MyPushButton::setNormalImage(const QString& normalImage){
    this->m_normalImage = normalImage;
     m_action = BntAction::normal;
     update();
}

void MyPushButton::setPressImage(const QString& pressImage){
    this->m_pressImage = pressImage;
}
void MyPushButton::setHoverImage(const QString& hoverImage){
    this->m_hoverImage = hoverImage;
}

void MyPushButton::setState(BntAction state){
    m_action = state;
}

int  MyPushButton::getState() const{
    return static_cast<int>(m_action);
}

void MyPushButton::paintEvent(QPaintEvent* ev){
    QString imagePath;
    if(m_action == BntAction::normal){
        imagePath = m_normalImage;
    }else if(m_action == BntAction::hover){
        imagePath = m_hoverImage;
    }
    else{
        imagePath = m_pressImage;
    }
    QPainter painter(this);
    QPixmap pix;
    pix.load(imagePath);
    this->setFixedSize(m_width,m_height);
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

}
